<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @Copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-26下午5:34:05
 *
 */
 
 /**
  * CanGoo Archive 控制器
  * 归档控制器
  * 默认首页列表，分类， 标签， 日期， 
  */
  class Archive extends CI_Controller{
  	
  	/**
  	 * 构造函数
  	 */
  	public function __construct(){
  		parent::__construct();
  		$this->load->helper('url');
  		$this->load->helper('common');
  		$this->load->library('session');
  		$this->load->library('blog/option');
  		$this->load->library('blog/post');
  		$this->load->library('blog/widget');
  		$this->load->library('pagination');
  		$this->load->model('blog/post_model');
  		
  	}
  	
  	/**
  	 * 默认执行首页， 文章列表
  	 */
  	public function index($page = 1){
  	
  		$data['title']='漠天博客';
	    $per = 2;
	    $segment = 4;
	    $this->_pagination(site_url('blog/archive/index'), $this->post_model->get_num(), $per, $segment );
	    /**
	     * 设置Post类的参数
	     */
	    $this->post->__set('current_page', $page);
	    $this->post->__set('per_page', $per);
	    $this->post->__set('content_type', 'index');
	    $this->post->run();
	    
  		$this->load->view('blog/archive_view', $data);
  		
  	}
	
	/**
	 * 分类归档
	 * @access public 
	 * @param string $category category slug
	 * @param int $page the page number
	 * @return  void description
	 */
	public function category($category, $page = 1){
		$category = trim($category);
		//如果$category为空，将category设置为默认分类
		if(!empty($category) || $category != ''){
			$category_obj = $this->post_model->get_one(array('tid', 'name'),'taxonomy',array('slug'=>$category,'module'=>'blog', 'type'=>'category'));
		}else{
			$category_obj = $this->post_model->get_one(array('tid', 'name'),'taxonomy',array('isDefault'=>1,'module'=>'blog', 'type'=>'category'));
		}
		$data['title'] = $category_obj->name;
	  	/**
	  	 * 分页配置
	  	 */	
	    $per = 2;
	    $segment = 4;
	    $this->_pagination(site_url('blog/archive/category/'.$category), $this->post_model->get_num($category_obj->tid, 'taxonomy'), $per, $segment );
	   	/**
	     * 设置Post类的参数
	     */
	    $this->post->__set('current_page', $page);
	    $this->post->__set('per_page', $per);
	    $this->post->__set('current_tid', $category_obj->tid);
	    $this->post->__set('content_type', 'taxonomy');
	    $this->post->run();
	    
	    $this->load->view('blog/archive_view', $data);
	    	
	}
	/**
	 * 标签归档
	 * @access public 
	 * @param string $tag tag slug
	 * @param int $page the page number
	 * @return  void description
	 */	
	public function tag($tag, $page = 1){
		$tag = trim($tag);
		//如果$tag为空，将tag设置为默认分类
		if(!empty($tag) || $tag != ''){
			$tag_obj = $this->post_model->get_one(array('tid', 'name'),'taxonomy',array('slug'=>$tag,'module'=>'blog', 'type'=>'tag'));
		}else{
			$tag_obj = $this->post_model->get_one(array('tid', 'name'),'taxonomy',array('isDefault'=>1,'module'=>'blog', 'type'=>'tag'));
		}
	  	/**
	  	 * 分页配置
	  	 */	
	    $per = 2;
	    $segment = 5;
	    $this->_pagination(site_url('blog/archive/tag/'.$tag), $this->post_model->get_num($tag_obj->tid, 'taxonomy'), $per, $segment );

	   	/**
	     * 设置Post类的参数
	     */
	    $this->post->__set('current_page', $page);
	    $this->post->__set('per_page', $per);
	    $this->post->__set('current_tid', $tag_obj->tid);
	    $this->post->__set('content_type', 'taxonomy');
	    $this->post->run();
	    
	    $this->load->view('blog/archive_view');
	    	
	}
	
	private function _pagination($url, $total, $per, $segment){
	  	/**
	  	 * 分页配置
	  	 */	
	    $config['base_url'] = $url;
	    $config['total_rows'] = $total;
	    $config['per_page'] = $per;
	    $config['num_links'] = 1;
	    $config['uri_segment'] = $segment;
	    $config['use_page_numbers'] = TRUE;
	    
	    $config['full_tag_open'] = '<div class="pagination">';
	    $config['full_tag_close'] = '</div>';
	    
	    $config['first_link'] = '首页';
	    $config['first_tag_open'] = '<span>';
	    $config['first_tag_close']='</span>';
	    
	    $config['last_link'] ='尾页';
	    $config['last_tag_open'] ='<span>';
	    $config['last_tag_close'] ='</span>';
	    
	    $config['prev_link'] = '上一页';
	    $config['prev_tag_open'] = '<span class="pre_page">';
	    $config['prev_tag_close'] ='</span>';
	    
	    $config['next_link'] = '下一页';
	    $config['next_tag_open'] = '<span class="next_page">';
	    $config['next_tag_close'] = '</span>';
	    
	    $config['cur_tag_open'] = '<span class="cur_page">';
	    $config['cur_tag_close'] ='</span>';
	    
	    $config['num_tag_open'] = '<span>';
	    $config['num_tag_close'] = '</span>';
	    
	    $this->pagination->initialize($config);		
	}
	
  }
  
  /* End of file archive.php */
  /* Location : ./apps/blog/controllers/archive.php */
